import {url} from './util.js'
const install = (Vue, vm) => {
	Vue.prototype.$u.http.setConfig({
		baseUrl: url,
		dataType: 'json',
		showLoading: true,
		loadingText: '努力加载中~',
		loadingTime: 500,
		loadingMask: true,
		originalData: true,
		// 设置自定义头部content-type
		header: {
			"x-requested-with": "XMLHttpRequest",
			"content-type": "application/x-www-form-urlencoded",
		},
	});
	// 请求拦截，配置Token等参数
	Vue.prototype.$u.http.interceptor.request = (config) => {
		var token = null,lang = null;
		token = uni.getStorageSync("token");
		lang = uni.getStorageSync('lang')
		config.header.token = token;
		config.header.lang = lang;
		return config; 
	}
	// 响应拦截，判断状态码是否通过
	Vue.prototype.$u.http.interceptor.response = ({data}) => {
		var token = null;
		token = uni.getStorageSync("token");
		if(data.code == 0) {
			return data.obj;  
		} else if(data.code == -2){
			vm.$u.toast("您没有登录");
		} else {
			vm.$u.toast(data.msg);
			return false;
		}
	}
}

export default {
	install
}